package com.elcom.player;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

import com.elcom.player.downloadmanager.HttpDownloader;
//import android.widget. LinearLayout;

public final class VideoGallery extends ListActivity  {
	private static final int PLAYSERVER_REQ    = 0x01;		//request code for intent
	private static FileManager 		 flmg;
	private EventHandler handler;
	private EventHandler.TableRow table;
	static Context 		mContext;
	private Handler mHandler = new Handler();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		mContext = this;
		new DownloadTask().execute();
		
		/*read settings*/	
		//readSettings();
		flmg = new FileManager();
		handler = new EventHandler(VideoGallery.this, flmg,Globals.MANAGER_TYPE_VIDEO);
		table = handler.new TableRow();
		/*sets the ListAdapter for our ListActivity and
		 *gives our EventHandler class the same adapter
		 */
		//handler.setListAdapter(table); 
		setListAdapter(table);
		registerForContextMenu(getListView());
		//mTimerUpdate = new TimerUpdate();
		//mHandler.postDelayed(mTimerUpdate, 100);
		
		
	}
	private class DownloadTask extends AsyncTask<Void, Integer, Void>{
		HttpDownloader mHttpDownloader;
		ProgressDialog mProgressDialog  ;
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			mProgressDialog = new ProgressDialog(VideoGallery.this);
			mProgressDialog.setMessage("Download Task");
			mProgressDialog.setIndeterminate(false);
			mProgressDialog.setMax(100);
			mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
			mProgressDialog.show();
		}
		@Override
		protected Void doInBackground(Void... params) {
			// TODO Auto-generated method stub
			URL url=null;
			try {
				url = new URL("http://channelz.hot1.cache11.vcdn.vn/zv/42d1d77e6a33d2d45689e1e2c3a1f73a/50b83d50/2012" +
						"/11/29/7/d/7d978b9fa0cdd6489a38db5e9a625ad4.mp4");
				
//				url = new URL("http://172.16.9.171/film/eee.MP4");
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			mHttpDownloader = new HttpDownloader(url, "/mnt/sdcard/", 8);
			mHttpDownloader.run();
			publishProgress((int)mHttpDownloader.getProgress());
			return null;
		}
		@Override
		protected void onProgressUpdate(Integer... values) {
			// TODO Auto-generated method stub
			super.onProgressUpdate(values);
			log(String.valueOf(values[0]));
			mProgressDialog.setProgress(values[0]);
		}
		@Override
		protected void onPostExecute(Void result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			mProgressDialog.dismiss();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stu b
		getMenuInflater().inflate(R.menu.menu_videoplayer, menu);
		return super.onCreateOptionsMenu(menu);
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method  stub
		switch (item.getItemId()) {
		case R.id.menu_playserver:
			//start activity play video on server
			toast("play-server");
			Intent i =new Intent(VideoGallery.this, playserver.class);
			//startActivityForResult(i, PLAYSERVER_REQ);
			startActivity(i);
			return true;
		case R.id.menu_settings:
            return true;
        case R.id.menu_fullscreen:
        	return true;
		}
		return super.onOptionsItemSelected(item);
	}
	@Override
	public void onPause(){
		super.onPause();
	}

	@Override
	public void onDestroy(){
		super.onDestroy();
	}

	/**
	 *  To add more functionality and let the user interact with more
	 *  file types, this is the function to add the ability. 
	 *  
	 *  (note): this method can be done more efficiently 
	 */
	//@Override
	public void onListItemClick(ListView parent, View view, int position, long id) {
		final String item = handler.getData(position);

		File file = new File(flmg.getCurrentDir() + "/" + item);

		if (FileManager.supportedFile(file.getPath())) {
			if(file.exists()){
				String filename =   file.getPath();
				Intent intent = null;
				intent = new Intent(VideoGallery.this, VideoPlayerActivity.class);
				intent.putExtra("videofilename", filename);
				startActivity(intent);
			}		
		}
	}
	public void log(String s){
		System.out.println(s);
	}
	public void toast(String ss){
		Toast.makeText(VideoGallery.this, ss, Toast.LENGTH_SHORT).show();
	}
}
